/**
 * Copyright © 2013 南京思创信息技术有限公司. *
 * 版权所有.
 *
 * 类名　　    : Readme
 * 功能概要    : 免责协议页面. * 做成日期    : 2013/06/04 wangxw
 */
package com.sevenit.nj.yrmtfish.login;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import com.sevenit.nj.yrmtfish.R;
import com.sevenit.nj.yrmtfish.common.AppManager;
import com.sevenit.nj.yrmtfish.common.StaticPara;
import com.sevenit.nj.yrmtfish.common.Utils;

/**
 * 免责协议页面的类
 * @author wangxw
 * @version 1.0
 */
@SuppressLint("HandlerLeak") 
public class Readme extends Activity {
	
	/**
	 * onCreate
	 */
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		//设置画面布局
		this.setContentView(R.layout.readme);
		
		//添加该Activity到AppManager
		AppManager.addActivity(this);
		
		//设置画面控件
		setControl();

		//初始化画面数据.
		initPageData(); 
	}
	
	/**
	 * 设置画面控件
	 */
	private void setControl() {
		
		//点击[复选框和文字]的响应.
		TextView txtSel = (TextView)findViewById(R.id.txt_sel1);
		txtSel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CheckBox check = (CheckBox)findViewById(R.id.check_select);
				if(check.isChecked()){
					check.setChecked(false);
				} else {
					check.setChecked(true);
				}
			}
		});
		
		txtSel = (TextView)findViewById(R.id.txt_sel2);
		txtSel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CheckBox check = (CheckBox)findViewById(R.id.check_select);
				if(check.isChecked()){
					check.setChecked(false);
				} else {
					check.setChecked(true);
				}
			}
		});
				
		//点击[同意]按钮的响应.
		ImageButton button = (ImageButton)findViewById(R.id.btn_ok);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CheckBox check = (CheckBox)findViewById(R.id.check_select);
				if(!check.isChecked()){
					return;
				}
				//保存配置文件
				StaticPara.MemConf.bShowReadme  = false;
				StaticPara.MemConf.saveMemConf();
				//打开主菜单画面
				//Intent intent = new Intent();
				if(!StaticPara.sysIsReady() || 
					Utils.isNull(StaticPara.MemConf.MyTel)){
					//intent.setClass(Readme.this, Setting.class);
				}else{
					//intent.setClass(Readme.this, MainMenu.class);
				}
				//startActivity(intent);
				//关闭本画面
				AppManager.closePage(Readme.class.getName());
			}
		});

		//设置[同意]按钮按下效果
		button.setOnTouchListener(new ImageButton.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(R.drawable.btn_tongyi_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(R.drawable.btn_tongyi);
				}
				return false;
			}
        });
		
		//点击[拒绝]按钮的响应.
		button = (ImageButton)findViewById(R.id.btn_cancel);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AppManager.sysExit();
			}
		});

		//设置[拒绝]按钮按下效果
		button.setOnTouchListener(new ImageButton.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(R.drawable.btn_jujue_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(R.drawable.btn_jujue);
				}
				return false;
			}
        });
	}
	
	/**
	 * 初始化画面数据.
	 */
	private void initPageData() {
		
		//读取readme文件
		try { 
			String strReadme = "";
			InputStreamReader inputReader = new InputStreamReader(
					getResources().getAssets().open("readme.txt"));
			BufferedReader bufReader = new BufferedReader(inputReader);
			String strLine;
			while((strLine = bufReader.readLine()) != null){
				strReadme += strLine + "\r\n";
			}
			inputReader.close();
			bufReader.close();
			/*byte[] buffer = new byte[1024];
			InputStream is = getResources().getAssets().open("readme.txt");
			while(is.read(buffer) > 0){
				strReadme += String.new String(buffer,, "GB2312");
			}
			is.close();*/
			TextView txtShow = (TextView)findViewById(R.id.txt_readme);
			txtShow.setText(strReadme);
		} catch (Exception e) {
			
		}
	}

	/**
	 * 监听手机硬件上的返回等按钮
	 */
	@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK &&	event.getRepeatCount() == 0) { 
            dialog(); 
            return false; 
        }
        return false; 
    }
	
	/**
	 * 退出确认.
	 */
	private void dialog() { 
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setMessage("确定要退出吗?"); 
        builder.setTitle("提示"); 
        builder.setPositiveButton("确定", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        AppManager.sysExit();
                    } 
                }); 
        builder.setNegativeButton("取消", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    }
}
